<template>
  <div class="logincode">
    <el-form ref="registerRef" :model="registerForm" class="register-form">
      <h3 class="title">若依后台管理系统</h3>
      <el-form-item prop="phonenumber">
        <el-input
          v-model="registerForm.phonenumber"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="手机号"
        >
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <Verify
          v-if="showVerify"
          @success="capctchaCheckSuccess"
          :mode="'fixed'"
          :captchaType="'blockPuzzle'"
          :phonenumber=registerForm.phonenumber
          :imgSize="{ width: '330px', height: '155px' }"
          ref="verify"
      ></Verify>
      <el-form-item prop="logincode">
        <el-input
          size="large"
          v-model="registerForm.logincode"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter="handleRegister"
        >
          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
        </el-input>
        <el-button type="primary" @click="getLoginCode">获取验证码</el-button>
<!--        <div class="register-code">-->
<!--          <img :src="codeUrl" @click="logincode" class="register-code-img"/>-->
<!--        </div>-->
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登录</span>
          <span v-else>登录中...</span>
        </el-button>
        <div style="float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 specialized.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Verify from "../components/Verifition/Verify.vue";
import {useRoute, useRouter} from "vue-router";
import useUserStore from '@/store/modules/user'
const showVerify = ref(false)
const router = useRouter();
const route = useRoute();
const redirect = ref(undefined);
const { proxy } = getCurrentInstance();

const registerForm = ref({
  userId: "",
  username: "",
  password: "",
  phonenumber:"",
  logincode:"",
  confirmPassword: "",
  code: "",
  uuid: ""
});
const userStore = useUserStore()
const codeUrl = ref("");
const loading = ref(false);
const captchaEnabled = ref(true);

components: {
  Verify
}

watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect;
}, { immediate: true });


function getLoginCode(){
  showVerify.value=true
}
function capctchaCheckSuccess(params) {

  showVerify.value=false
  // Set the captcha verification code from params
  // getlogincode(registerForm.value.phonenumber).then(
  //     res => {
  //       console.log(res)
  //       alert(res.data)
  //
  //     }
  // )


  // Set loading state to true
  // this.loading = true;
  //
  // // Dispatch login action using Vuex store, passing loginForm data
  // this.$store.dispatch("Login", this.loginForm)
  //     .then(() => {
  //       // After successful login, redirect to specified path or default to "/"
  //       this.$router.push({ path: this.redirect || "/" }).catch(() => {});
  //
  //     })
  //     .catch(() => {
  //       // If login fails, set loading state to false
  //       this.loading = false;
  //     });
}
function handleLogin() {
  userStore.login(registerForm.value).then(() => {
    const query = route.query;
    const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
      if (cur !== "redirect") {
        acc[cur] = query[cur];
      }
      return acc;
    }, {});
    router.push({ path: redirect.value || "/", query: otherQueryParams });
  }).catch(() => {
    loading.value = false;
    // 重新获取验证码
    if (captchaEnabled.value) {
      getCode();
    }
  });

}
function getCode() {

  getCodeImg().then(res => {
    captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.data.img;
      registerForm.value.uuid = res.data.uuid;
    }
  });
}


getCode()

</script>

<style lang='scss' scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 40px;
  padding-left: 12px;
}
</style>
